package cn.sczc.jh.client.controller.sys;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.sczc.jh.client.base.AbstractController;
import cn.sczc.jh.client.entity.Role;
import cn.sczc.jh.client.service.sys.RoleService;

@RestController
@RequestMapping("/sys/roles")
public class RoleController extends AbstractController {

	@Autowired
	private RoleService roleService;

	/**
	 * 获取用户列表
	 * 
	 * @param modelMap
	 * @param params
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.GET)
	public ResponseEntity<ModelMap> list(ModelMap modelMap, @RequestParam Map<String, Object> params) {
		@SuppressWarnings("unchecked")
		Page<Role> page = (Page<Role>) super.getPage(params);
		Page<Role> data = roleService.getRoleByParams(page, params);
		return setSuccessModelMap(modelMap, data);
	}

	/**
	 * 添加角色
	 * 
	 * @param modelMap
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.POST)
	public ResponseEntity<ModelMap> add(ModelMap modelMap, @RequestBody Role role) {
		int count = roleService.addRole(role);
		return setSuccessModelMap(modelMap, count);
	}

	/**
	 * 更新角色
	 * 
	 * @param modelMap
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.PUT)
	public ResponseEntity<ModelMap> update(ModelMap modelMap, @RequestBody Role role) {
		int count = roleService.updateRole(role);
		return setSuccessModelMap(modelMap, count);
	}

	/**
	 * 删除角色
	 * 
	 * @param modelMap
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<ModelMap> del(ModelMap modelMap, @PathVariable Long id) {
		int count = roleService.delById(id);
		return setSuccessModelMap(modelMap, count);
	}

	@RequestMapping(value="/per", method = RequestMethod.POST)
	public ResponseEntity<ModelMap> setPermission(ModelMap modelMap, @RequestBody Role role) {
		roleService.setPermission(role);
		return setSuccessModelMap(modelMap);
	}
}
